package lucky.xiong.api.List;

import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * @author XiongJianLin
 * @date Created in 2022/5/10 16:54
 */
public class 对象属性排序 {
    public static void main(String[] args) {
        List<People> peoples = new ArrayList<>();
        for (int i = 0; i <10; i++){
            People people = new People();
            people.setName("姓名"+i);
            people.setAge(new Random().nextInt(10));
            peoples.add(people);
        }
        System.out.println("原始：");
        peoples.forEach(System.out::println);
        System.out.println("新对象-》排序后：");
        peoples.stream().sorted(Comparator.comparing(People::getAge))
                .forEach(System.out::println);

        // 或者 先排序age，再排name倒叙。
        peoples.sort(Comparator.comparing(People::getAge)
                .thenComparing(Comparator.comparing(People::getName).reversed())
        );
        System.out.println("源对象-》排序后：");
        peoples.forEach(System.out::println);
    }


    @Data
    static
    class People{
        public String name;
        public Integer age;
    }



}
